/**
 *  Render工具类;
 * @author    KeJun Zheng
 * @copyright (c) 2011,  WayLink
 * @version   0.1
 * @date      <ul>
 * <li>2013-08-07</li>
 * </ul>
 *
 */

Ext.data.Types.STRING.convert = function(v){
	return (v == undefined || v == null || v == "null" || v == "undefined") ? '' : String(v);   
} ;
Ext.data.Types.INT.convert = function(v){
    return (v == undefined || v == null || v == '' || v == "null" || v == "undefined")?'':
            parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10);
} ;



var RenderUtil = function(){};



/**
 * render as mask chars'
 * @duplicated replaced by getFix({mask:"*",size:6})
 * @param char, size
 * @returns {Function}
 */
RenderUtil.getMask = function(char,size) {
	var display='';
	var num = parseInt(size)||6;
	var chr = char||'*';
	for(var i=0;i<num;i++){
		display+=chr;
	}
	return function(value) {
		return display;
	};
};
/**
 * date render using given format, default is 'Y-m-d H:i:s'
 * @param format
 * @returns {Function}
 */
RenderUtil.getDateTime = function(format) {
	return function(value) {
		var display = "", fmt = format||'c';
		if (Ext.isEmpty(value)) {
			display = "";
		} else if (Ext.isEmpty(value.time)) {
			if( typeof(value.format)=='function' ){	//if value from dateField,
				display = value.format(fmt);
			}else if(Ext.isDate(value)){
				display = value.format(fmt);
				//display = value.toDateString();
			}else if(typeof(value)=='string'){
				display = value.replace(/T/g, ' ');// 去掉日期类型数据中的特殊字符T
				try {
					var dt = Date.parseDate(value, fmt);
				    if(!dt) {
				      var altFormats="c|Y-m-d H:i:s|Y-m|H:i:s|H:i|m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j";
				      var fmtList =  altFormats.split("|");
				        for (var i = 0, len = fmtList.length; i < len && !dt; i++) {
				            dt = Date.parseDate(value,fmtList[i]);
				        }
				    }
				    if(dt){display = dt.format(fmt);}
				} catch (e) {alert(e);}
			}
		} else {
			display = value;
			try {
				var dt =null;
				if((typeof(value))=="int"){
					dt=new Date(value);
				}else{
					dt=new Date(value.time);
				}
				
				display = dt.format(fmt);
			} catch (e) {alert(e);}
		}
		return display;
	};
};


RenderUtil.getValue = function(obj) {
	var val = obj, fmt = 'Y-m-d\\TH:i:s';//JSONInterceptor using JSONUtil.RFC3339_FORMAT
	if (Ext.isEmpty(obj)) {
		val = "";
	}else if(typeof(obj)=='object'){
		 if (Ext.isEmpty(obj.time)) {
			if( typeof(obj.format)=='function' ){
				val = obj.format(fmt);
			}else if(Ext.isDate(obj)){
				val = obj.format(fmt);
				//val = obj.toDateString();//Wed Nov 5 00:00:00 UTC 0800 2014
			}
		} else {
			try {
				val = new Date(obj.time).format(fmt);
			} catch (e) {}
		}
	}
	return val;
};

RenderUtil.format = function(value,pattern){
	var v=value;
	try{//for number;
		v=Ext.util.Format.number(value,pattern);
	}catch(e){
		alert(e);
	}
	return v;
};

//table content add title 
RenderUtil.getTitle=function(conf){
	return function(value, metaData, record, rowIndex, colIndex, store) {
		var vt=(value==null)?'':value;
		var vf=(conf&&conf.format)?RenderUtil.format(vt,conf.format):vt;
		var str = '<span title="'+vt+'">'+vf+'</span>';
		return str;
	};
};
//table content add title 
RenderUtil.getLink=function(conf){
	return function(value, metaData, record, rowIndex, colIndex, store) {
		var v = (value?value:'');
		if( v ){
			v = '<a href="javascript:window.parent.showPageAsTab(\'信息:'+v+'\',\'../base/detail.jsp?nid='+conf.links+'&sid='+v+'\',\'detail_'+v+'\');void(0)">'+v+'</href>';
		}
		return v;
	};
};

RenderUtil.getMap=function(conf){
	var linksId=conf.links;
	return function(value, metaData, record, rowIndex, colIndex, store) {
		if(Ext.isEmpty(value)){ return ''; }
		var map=conf["enums"]["map"];
		return map[value];
	};
};

RenderUtil.getFix=function(attr){
	var	conf = attr||{};
	return function(value, metaData, record, rowIndex, colIndex, store) {
		if(Ext.isEmpty(value)){
			return '';
		}
		return (conf.prefix||'')+RenderUtil.format(value,conf.format)+(conf.unit||'')+(conf.suffix||'');
	};
};

RenderUtil.getUnit=function(unit){
	return RenderUtil.getFix({unit:unit});
};


//table content add title 
RenderUtil.title=function(val){ 
	var v=(val==null)?'':val;
	v = '<span title="'+v.replace(/"/g,"'")+'">'+v+'</span>';
	return v;
};

RenderUtil.get=function(dtype,attr){
	var	conf = attr||{};
	var func = RenderUtil.getTitle(conf);//getValue;
	if(conf.mask){
		func = RenderUtil.getMask(conf.mask, conf.size);
	}else if( conf.prefix||conf.suffix||conf.unit){
		func = RenderUtil.getFix(conf);
	}else if(conf.enums){
		func = RenderUtil.getMap(conf);
	}else if(conf.links){
		func = RenderUtil.getLink(conf);
	}else switch(dtype){
		case 91:
			func = RenderUtil.getDateTime('Y-m-d');
			break;
		case 92:
			func = RenderUtil.getDateTime('H:i:s');
			break;
		case 93:
			func = RenderUtil.getDateTime('Y-m-d H:i:s');
			break;
		default:
	}
	return func;
};

RenderUtil.onlyDate=RenderUtil.getDateTime('Y-m-d');
RenderUtil.onlyTime=RenderUtil.getDateTime('H:i:s');
RenderUtil.dateTime=RenderUtil.getDateTime('Y-m-d H:i:s');


